{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a Postgres Chat Agent in AGiXT with ezLocalai Provider\n",
    "\n",
    "This example assumes that you have your AGiXT and ezLocalai servers set up and running. This example is specifically for running AGiXT with ezLocalai and training a local agent.\n",
    "\n",
    "If you do not have ezLocalai set up and you want to set it up to run local models, go to https://github.com/DevXT-LLC/ezlocalai and follow the instructions there to set up the server, then continue with this example.\n",
    "\n",
    "## Create the Agent\n",
    "\n",
    "For this example, we will create an agent that will turn your natural language questions into SQL queries that get executed, then a response with either a CSV of data or a string response.\n",
    "\n",
    "Connect to any Postgres database by updating the agent's settings. Modify the following and any others as needed:\n",
    "\n",
    "- `agixt_server`\n",
    "- `api_key`\n",
    "- `agent_name`\n",
    "- `EZLOCALAI_API_URL`\n",
    "- `EZLOCALAI_API_KEY`\n",
    "- `POSTGRES_DATABASE_HOST`\n",
    "- `POSTGRES_DATABASE_PORT`\n",
    "- `POSTGRES_DATABASE_NAME`\n",
    "- `POSTGRES_DATABASE_USERNAME`\n",
    "- `POSTGRES_DATABASE_PASSWORD`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agixtsdk import AGiXTSDK\n",
    "\n",
    "agixt_server = \"http://localhost:7437\"  # Change this to your AGiXT server URL\n",
    "api_key = \"None\"  # Change this to your AGiXT API key\n",
    "\n",
    "agixt = AGiXTSDK(base_uri=agixt_server, api_key=api_key)\n",
    "\n",
    "agent_name = \"Postgres\"  # Change this if desired\n",
    "\n",
    "agixt.add_agent(\n",
    "    agent_name=agent_name,\n",
    "    settings={\n",
    "        \"provider\": \"ezlocalai\",  # LLM Provider\n",
    "        \"transcription_provider\": \"ezlocalai\",  # Voice transcription provider, default uses the built in transcription in AGiXT.\n",
    "        \"translation_provider\": \"ezlocalai\",  # Voice translation provider, default uses the built in translation in AGiXT.\n",
    "        \"embeddings_provider\": \"default\",  # Embeddings provider, default uses the built in embeddings in AGiXT.\n",
    "        \"image_provider\": \"None\",  # If set, AGiXT will autonomously create images if it chooses to do so based on the conversation.\n",
    "        \"vision_provider\": \"ezlocalai\",  # Vision provider, None means no vision capabilities. We will use OpenAI's since we're using GPT-4o.\n",
    "        \"tts_provider\": \"None\",  # The responses in Postgres Chat will often be CSV format, so we don't need TTS.\n",
    "        \"AI_MODEL\": \"ezlocalai\",  # It doesn't matter which model you put here, ezlocalai uses the model it was started with.\n",
    "        \"EZLOCALAI_API_URI\": \"http://ezlocalai:8091/v1/\",  # URL for the EZLOCALAI API, change this to your EZLOCALAI API URL. Never use localhost here, it is a different container.\n",
    "        \"EZLOCALAI_API_KEY\": \"Your EZLOCALAI API key\",  # Change this to your EZLOCALAI API key\n",
    "        \"MAX_TOKENS\": 4096,\n",
    "        \"AI_TEMPERATURE\": \"0.7\",\n",
    "        \"AI_TOP_P\": \"0.95\",\n",
    "        \"mode\": \"chain\",  # For info about chat completion modes, go to https://josh-xt.github.io/AGiXT/2-Concepts/04-Chat%20Completions.html\n",
    "        \"chain_name\": \"Postgres Chat\",\n",
    "        \"chain_args\": \"{}\",\n",
    "        \"POSTGRES_DATABASE_HOST\": \"postgres\",  # Change this to your Postgres database host\n",
    "        \"POSTGRES_DATABASE_NAME\": \"postgres\",  # Change this to your Postgres database name\n",
    "        \"POSTGRES_DATABASE_PORT\": \"5432\",  # Change this to your Postgres database port\n",
    "        \"POSTGRES_DATABASE_USERNAME\": \"postgres\",  # Change this to your Postgres database username\n",
    "        \"POSTGRES_DATABASE_PASSWORD\": \"postgres\",  # Change this to your Postgres database password\n",
    "    },\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat with the Agent\n",
    "\n",
    "Turn your natural language queries into SQL queries that are executed on your Postgres database, then return the results in a CSV format or a string response."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "prompt = \"How many people bought services in 2024?\"\n",
    "\n",
    "openai.base_url = f\"{agixt_server}/v1/\"\n",
    "openai.api_key = api_key\n",
    "openai.api_type = \"openai\"\n",
    "\n",
    "response = openai.chat.completions.create(\n",
    "    model=agent_name,\n",
    "    messages=[{\"role\": \"user\", \"content\": prompt}],\n",
    "    user=\"Services query\",  # This field is used for the conversation name, if empty, it will use today's date\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# That is all!\n",
    "\n",
    "You now have a Postgres Chat Agent in AGiXT with ezLocalai Provider. You can now chat with your agent and get responses from your Postgres database."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
